<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <!--<el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="标题"-->
      <!--v-model="listQuery.name">-->
      <!--</el-input>-->
      <!--<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>-->
      <el-button class="filter-item btn-ww" style="margin-left: 10px;" @click="handleCreate" type="primary"
                 icon="el-icon-plus">添加
      </el-button>
      <!--<el-button class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>-->
    </div>

    <el-table class="table-border color-none" border :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="正在努力加载中。。。"  fit
              highlight-current-row
              style="width: 100%">
      <el-table-column align="center" label="NO." width="65" type="index">
      </el-table-column>
      <el-table-column width="0px"  style="display: none;" v-if="false">
        <template slot-scope="scope">
          <span style="display: none;">{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="150px" label="门站名称" align="center">
        <template slot-scope="scope">
          <span class="link-type" @click="handleUpdate(scope.row)">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="负责人">
        <template slot-scope="scope">
          <span>{{scope.row.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="手机号">
        <template slot-scope="scope">
          <span>{{scope.row.userMobile}}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{scope.row.creation}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" class-name="small-padding">
        <template slot-scope="scope">
          <el-button class="btn-ww" icon="el-icon-edit-outline" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button class="btn-ww" icon="el-icon-delete" size="mini" type="danger"
                     @click="handleModifyStatus(scope.row,'deleted')">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background @current-change="handleCurrentChange"
                   :current-page.sync="listQuery.pageNumber"
                   :page-size="listQuery.pageSize"
                   layout="prev, pager, next"
                   :total="total">
    </el-pagination>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="70px"
               style='width: 400px; margin-left:50px;'>
        <el-form-item label="分组名" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>
        <!--<el-form-item label="门站地址" prop="address">-->
          <!--<el-input v-model="temp.address"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item  label="用户名" prop="userId">
          <el-select v-model="temp.userId" placeholder="请选择用户">
             <el-option :key="user.id" :label="user.name" :value="user.id" v-for="(user,index) in users">{{user.name}}</el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确 定</el-button>
        <el-button v-else type="primary" @click="updateData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getPagesList, addOrEditGroup, updateGroup, delGroup } from '../../../api/group'
  import { getAllUser } from '../../../api/user'

  import { parseTime } from '../../../utils'

  export default {
    name: 'complexTable',
    directives: {},
    data() {
      return {
        tableKey: 0,
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          pageNumber: 1,
          pageSize: 10
        },
        users: [],
        importanceOptions: [1, 2, 3],
        sortOptions: [{ label: '按ID升序列', key: '+id' }, { label: '按ID降序', key: '-id' }],
        showAuditor: false,
        temp: {
          companyId: this.$store.state.user.companyId,
          name: '',
          userId: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          name: [{ required: true, message: '名称必填', trigger: 'blur' }]
        }
      }
    },
    filters: {},
    created() {
      this.getList()
      getAllUser().then(data => {
        this.users = data.data
      })
    },
    methods: {
      getList() {
        this.listLoading = true
        getPagesList(this.listQuery).then(response => {
          this.list = response.data.content
          this.total = response.data.totalElements
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.pageNumber = val
        this.getList()
      },
      handleModifyStatus(row, status) {
        this.handleDelete(row)
        // this.$message({
        //   message: '操作成功',
        //   type: 'success'
        // })
        row.status = status
      },
      resetTemp() {
        this.temp = {
          name: '',
          userId: '',
          id: ''
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addOrEditGroup(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      parseTime: parseTime,
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        // this.$nextTick(() => {
        //   this.updateData()
        // })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            console.log(JSON.stringify(tempData))

            updateGroup(tempData).then(() => {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm('是否删除该门站?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delGroup({ id: row.id }).then(() => {
            this.list.splice(row.index, 1)
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
</script>
